$(function () {
    $('#usernickname').focus().blur(checknickname);
    $("#register-sub-btn").click(function () {
        if (checknickname() && checkpasswd()) {
            // 获取默认地址
            var loading = weui.loading('loading', {
                className: 'custom-classname'
            });
            var nickname = $('#usernickname').val();

            var passwd = $('#passwd').val();
            var school = $("#item-school").text();
            var result = Model.user.register({
                nickname: nickname,
                passwd: passwd,
                school: school
            });
            if (result.code == 1) {
                loading.hide();
                weui.toast('注册成功', {
                    duration: 2000,
                    className: 'custom-classname',
                    callback: function () {
                        location.href = 'login.html';
                    }
                });

            } else {
                loading.hide();
                weui.toast('注册失败，请重新注册', {
                    duration: 1500,
                    className: 'custom-classname',
                    callback: function () {
                        location.reload()
                    }
                });
            }
        }

    });

    function checknickname() {
        //判断数据库中是否存在
        var nickname = $('#usernickname').val();
        var result = Model.user.validateNickname({
            nickname: nickname,
        });
        if (result.code == 0) {
            weui.alert("用户名已存在");
            return false;
        }
        var nickname = $('#usernickname').val();
        if (nickname == null || nickname == "") {
            // 输入不合法
            weui.alert('账户名不能为空');
            return false;
        }
        var reg = /^\w{3,10}$/;
        if (!reg.test(nickname)) {
            weui.alert("请输入3-10个字母或数字或下划线");
            return false;
        }
        $('#usernickname').empty();
        return true;
    }

//判断密码是否符合要求
    function checkpasswd() {
        var passwd = $('#passwd').val();
        var confirm = $('#confirm-passwd').val();
        if (passwd == null || passwd == "") {
            //提示错误
            weui.alert('密码不能为空');
            return false;
        }

        var reg = /^\w{3,10}$/;
        if (!reg.test(passwd)) {
            weui.alert("请输入3-10个字母或数字或下划线");
            return false;
        }
        if (passwd != confirm) {
            //提示错误
            weui.alert('两次密码输入不一致');
            return false;
        }
        var school = $("#item-school").text();
        if (school == null || school == "") {
            weui.alert('请选择学校');
            return false;
        }

        return true;
    }

    $('#selectSchool').on('click', function () {
        weui.picker(schoolList.data, {
            className: 'custom-classname',
            container: 'body',
            defaultValue: [1, 1, 1],
            onChange: function (result) {
                console.log(result)
            },
            onConfirm: function (result) {
                $('#item-school').text(result[2].label);
            },
            id: 'singleLinePicker'
        });
    });
    $("#weuiAgree").change(function () {
        if ($(this).is(':checked')) {
            $('#register-sub-btn').removeClass('weui-btn_plain-disabled');
            $('#register-sub-btn').addClass('login-submit');

        } else {
            $('#register-sub-btn').addClass('weui-btn_plain-disabled');
            $('#register-sub-btn').removeClass('login-submit');
        }
    });

    //显示协议
    $(document).on('click', '.agreement-btn', function () {
        $('.shade').css('display', 'block');
        $('.xieyi-content').css('display', 'block');
        $('.xieyi-content').html(Common.pa._MESSAGE);
    });
    // 关闭协议按钮
    $(document).on('click', '.xieyi-close-btn', function () {
        $('.shade').css('display', 'none');
        $('.xieyi-content').css('display', 'none');
    });

})